<?php
namespace RMD\AccountBundle\Entity;
use Symfony\Component\Yaml\Yaml;
/* 
 * Manage segment options
 * both static (from config file) and dynamic (groups)
 */
class ListSegment
{
	private $options = array();
	private $list;
	public function __construct(Lists $list, $configPath = null) {
		if (!$configPath) {
			$configPath = __DIR__ . '/../Resources/config/segmentRules.yml';
		}
		$this->options = Yaml::parse($configPath);
		if (!is_array($this->options)) {
			throw new Exception('Cant load config file in ' . __CLASS__);
		}
		$this->list = $list;
		$this->setGroupSets();
	}
	public function getOptions(){
		return $this->options;
	}
	public function getOption($name){
		if (array_key_exists($name, $this->options)) {
			return $this->options[$name];
		} else {
			throw new Exception('Option ' . $fieldName . ' is not supported in ' . __CLASS__);
		}
	}
	private function setGroupSets(){
		foreach ($this->list->getGroupSets() as $groupSet) {
			$this->addGroupSet($groupSet);
		}
	}

	private function addGroupSet($group){
		$name = 'group_' . $group->getId();
		if (isset($this->options[$name])) {
			return $this;
		}
		$this->options[$name]['Title'] = $group->getTitle();
		$this->options[$name]['tableName'] = 'subscribers_groups';
		$this->options[$name]['columnName'] = 'group_id';
		$this->options[$name]['statements'] = array('EQUAL' => 'in group', 'NOT_EQUAL' => 'not in group');
		return $this;
	}
}